package com.maodou.core.model

import com.squareup.moshi.JsonClass

/**
 * 字典数据模型
 * 用于存储后台配置的字典数据，如状态码、类型码等
 */
@JsonClass(generateAdapter = true)
data class DictionaryData(
    val id: String? = null,
    val type: String, // 字典类型，如：user_status, order_status, payment_type 等
    val code: String, // 字典编码
    val name: String, // 字典名称/显示文本
    val value: String? = null, // 字典值（可选）
    val description: String? = null, // 描述
    val sort: Int = 0, // 排序
    val status: Int = 1, // 状态：1-启用，0-禁用
    val parentCode: String? = null, // 父级编码（用于层级结构）
    val level: Int = 1, // 层级
    val extra: Map<String, Any> = emptyMap(), // 扩展字段
    val createTime: Long? = null,
    val updateTime: Long? = null
) {
    /**
     * 检查是否启用
     */
    fun isEnabled(): Boolean {
        return status == 1
    }
    
    /**
     * 检查是否为根节点
     */
    fun isRoot(): Boolean {
        return parentCode.isNullOrEmpty()
    }
    
    /**
     * 获取显示值（优先使用name，其次value，最后code）
     */
    fun getDisplayValue(): String {
        return name.ifEmpty { value ?: code }
    }
    
    /**
     * 获取扩展字段值
     */
    fun getExtraValue(key: String): Any? {
        return extra[key]
    }
    
    /**
     * 获取扩展字段字符串值
     */
    fun getExtraString(key: String, defaultValue: String = ""): String {
        return extra[key] as? String ?: defaultValue
    }
    
    /**
     * 获取扩展字段整数值
     */
    fun getExtraInt(key: String, defaultValue: Int = 0): Int {
        return extra[key] as? Int ?: defaultValue
    }
    
    /**
     * 获取扩展字段布尔值
     */
    fun getExtraBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return extra[key] as? Boolean ?: defaultValue
    }
    
    companion object {
        // 常用字典类型常量
        const val TYPE_USER_STATUS = "user_status"
        const val TYPE_ORDER_STATUS = "order_status"
        const val TYPE_PAYMENT_TYPE = "payment_type"
        const val TYPE_GENDER = "gender"
        const val TYPE_EDUCATION = "education"
        const val TYPE_INDUSTRY = "industry"
        const val TYPE_REGION = "region"
        const val TYPE_DEPARTMENT = "department"
        const val TYPE_POSITION = "position"
        
        // 常用状态码
        const val STATUS_ENABLED = 1
        const val STATUS_DISABLED = 0
        
        /**
         * 创建简单字典项
         */
        fun create(type: String, code: String, name: String): DictionaryData {
            return DictionaryData(
                type = type,
                code = code,
                name = name
            )
        }
        
        /**
         * 创建带值的字典项
         */
        fun createWithValue(type: String, code: String, name: String, value: String): DictionaryData {
            return DictionaryData(
                type = type,
                code = code,
                name = name,
                value = value
            )
        }
    }
}